/**
 * Created by gongxun on 17/2/24.
 */

var GameRoomRuleHelper = function () {
    var that = {};

    that.key = "ruleConig";

    //规则改变位数+1
    that.ruleResetKey = "ruleConfigChangeKey38";

    that.defaultIndexKwx = 0;

    that.defaultIndexNn = 0;

    that.defaultIndexGsbdx = 0;

    that.defaultIndexPsz = 0;

    that.defaultIndexWhmj = 0;

    that.defaultIndexDzpk = 0;

    that.defaultIndexDtj = 0;

    that.defaultIndexPdk = 0;

    //拼三张可选规则
    that.pszRuleStruct = null;

    that.init = function () {
        that.resetRuleConfig();

        if (mo.gameHelper.projectCode == 0) {
            that.defaultIndexKwx = 3;
            that.defaultIndexNn = 1;
            that.defaultIndexGsbdx = 0;
            that.defaultIndexPsz = 0;
            that.defaultIndexWhmj = 0;
        } else if (mo.gameHelper.projectCode == 1) {
            that.defaultIndexKwx = 1;
            that.defaultIndexNn = 0;
            that.defaultIndexGsbdx = 0;
            that.defaultIndexPsz = 1;
            that.defaultIndexWhmj = 0;
        } else if (mo.gameHelper.projectCode == 2) {
            that.defaultIndexKwx = 3;
            that.defaultIndexNn = 1;
            that.defaultIndexGsbdx = 0;
            that.defaultIndexPsz = 1;
            that.defaultIndexWhmj = 0;
        }
    };

    //加载规则 如果本地有存储就调用 没有就用默认的规则
    that.loadRuleConfig = function (gameName) {
        var config = that.readRuleConfig(gameName);
        if (config == null) {
            if (gameName == "kwx") {
                config = that.loadKwxRuleConfig(that.defaultIndexKwx);
            } else if (gameName == "nn") {
                config = that.loadNnRuleConfig(that.defaultIndexNn);
            } else if (gameName == "gsbdx") {
                config = that.loadGsbdxRuleConfig(that.defaultIndexGsbdx);
            } else if (gameName == "psz") {
                config = that.loadPszRuleConfig(that.defaultIndexPsz);
            } else if (gameName == "whmj") {
                config = that.loadWhmjRuleConfig(that.defaultIndexWhmj);
            } else if (gameName == "dzpk") {
                config = that.loadDzpkRuleConfig(that.defaultIndexDzpk);
            } else if (gameName == "dtj") {
                config = that.loadDtjRuleConfig(that.defaultIndexDtj);
            } else if (gameName == "pdk") {
                config = that.loadPdkRuleConfig(that.defaultIndexPdk);
            } else {
                cc.log("error... 没有规则");
            }
        }

        return config;
    };

    //创建之后保存选项到本地
    that.saveRuleConfig = function (gameName, ruleConfig) {
        var key = that.getRuleConfigKey(gameName);
        mo.fileHelper.setItem(key, JSON.stringify(ruleConfig));
    };

    //重置规则
    that.resetRuleConfig = function () {
        var value = mo.fileHelper.getItem(that.ruleResetKey);
        if (value == undefined) {
            mo.fileHelper.removeItem(that.getRuleConfigKey("kwx"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("nn"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("gsbdx"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("psz"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("whmj"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("dzpk"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("dtz"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("dtj"));
            mo.fileHelper.removeItem(that.getRuleConfigKey("pdk"));

            mo.fileHelper.setItem(that.ruleResetKey, "1");

            //
            cc.log("重置了规则");
        }
    };

    that.readRuleConfig = function (gameName) {
        var config = null;

        var key = that.getRuleConfigKey(gameName);
        var itemValue = mo.fileHelper.getItem(key);
        if (itemValue != undefined) {
            config = JSON.parse(itemValue);
        }
        return config;
    };

    that.getRuleConfigKey = function (gameName) {
        return that.key + "_" + gameName;
    };

    that.getLODValue = function (gameName) {
        if (gameName == "kwx") {
            return [50, 100];
        } else if (gameName == "nn") {
            return [100, 200];
        } else if (gameName == "gsbdx") {
            return [100, 200];
        } else if (gameName == "psz") {
            return [100, 200];
        } else if (gameName == "whmj") {
            return [100, 200];
        } else if (gameName == "dzpk") {
            return [];
        } else if (gameName == "dtj") {
            return [];
        } else if (gameName == "pdk") {
            return [];
        } else {
            return [];
        }
    };

    //拼三张规则
    that.getPszRuleStruct = function (config) {
        if (that.pszRuleStruct == null) {
            //开始构造
            that.pszRuleStruct = [];

            //广水
            var item = {};
            item.maxpan = [6, 12];
            item.open = [5, 10, 15];
            item.bi = [1, 2, 3];
            item.men = [3, 5, 7];
            item.aaap = [10];
            that.pszRuleStruct.push(item);

            //大众
            item = {};
            item.maxpan = [6, 12];
            item.open = [8, 10, 15];
            item.bi = [2, 3, 4];
            item.men = [1, 2, 3];
            item.aaap = [0, 3, 5];
            that.pszRuleStruct.push(item);
        }

        return that.pszRuleStruct[config.ruleid - 1];
    };

    that.loadKwxRuleConfig = function (index) {
        var config = {};
        //基础规则
        config.maxpan = 8;
        config.playerCount = 3;
        config.piao = 1;
        config.kwxrate = 2;
        config.pengpengrate = 2;
        config.gangpaorate = 2;
        config.alltop = 8;
        config.Huang = 1;
        config.zm = 0;

        //扩展选项
        if (index == 0) {
            //孝感
            config.ruleid = 1;
            config.kan = 1;
            config.ting12 = 1;
            config.horse = 0;
        } else if (index == 1) {
            //襄阳
            config.ruleid = 2;
            config.horse = 0;
            config.pd = 0;

        } else if (index == 2) {
            //十堰
            config.ruleid = 3;
            config.sl = 1;
            config.cj = 1;
            config.pd = 0;
            config.horse = 0;
        } else if (index == 3) {
            //随州
            config.ruleid = 4;
            config.horse = 1;
        }

        return config;
    };

    that.loadNnRuleConfig = function (index) {
        var config = {};

        if (index == 0) {
            //大众玩法
            config.ruleid = 1;
            config.basepoint = 3;
            config.maxpan = 10;
            config.hosttype = 0;
            config.kou = 0;
            config.pay = 1;
            config.pwd = 0;
            config.addratetime = 0;
        } else if (index == 1) {
            //随州玩法
            config.ruleid = 2;
            config.basepoint = 3;
            config.maxpan = 10;
            config.hosttype = 0;
            config.kou = 0;
            config.pay = 1;
            config.pwd = 0;
            config.addratetime = 0;

        }

        return config;
    };

    that.loadGsbdxRuleConfig = function (index) {
        var config = {};

        if (index == 0) {
            //大众玩法
            config.ruleid = 1;
            config.basepoint = 3;
            config.maxpan = 10;
            config.hosttype = 0;
            config.pay = 1;
            config.pwd = 0;
            config.addratetime = 0;
        } else if (index == 1) {
            //河南玩法
            config.ruleid = 3;
            config.basepoint = 3;
            config.maxpan = 10;
            config.hosttype = 0;
            config.pay = 1;
            config.pwd = 0;
            config.addratetime = 0;
        }

        return config;
    };

    that.loadPszRuleConfig = function (index) {
        var config = {};

        if (index == 0) {
            //广水
            config.ruleid = 1;
            config.basepoint = 5;
            config.maxpan = 6;
            config.hosttype = 1;
            config.men = 3;
            config.bi = 1;
            config.open = 5;
            config.pwd = 0;
            config.aaap = 10;
            config.ssc = 1;
        } else if (index == 1) {
            //大众
            config.ruleid = 2;
            config.basepoint = 1;
            config.maxpan = 6;
            config.hosttype = 1;
            config.men = 1;
            config.bi = 2;
            config.open = 8;
            config.pwd = 0;
            config.aaap = 0;
            config.ssc = 1;
        }

        return config;
    };

    that.loadWhmjRuleConfig = function (index) {
        var config = {};

        if (index == 0) {
            config.ruleid = 1;
            config.basepoint = 1;
            config.alltop = 16;
            config.maxpan = 6;
            config.openrate = 1;
            config.huscore = 16;
            config.vip = 0;
            config.pwd = 1;
            config.yl = 0;
            config.fl = 0;
        }

        return config;
    };

    that.loadDzpkRuleConfig = function (index) {
        var config = {};

        if (index == 0) {
            config.ruleid = 1;
            config.basepoint = 2;
            config.maxpan = 5;
            config.vip = 0;
            config.pwd = 1;
            config.men = 0;
            config.lod = 0;
        }

        return config;
    };

    that.loadDtjRuleConfig = function (index) {
        var config = {};

        if (index == 0) { //大牌九
            config.ruleid = 1; //规则id 1：鬼子+天九王 2：都没有 3：鬼子 4：天九王

            config.guo = 0; // 0 没有锅  1 有锅
            config.xuguo = 0;  // 0 没有续锅  1 有续锅
            config.guomax = 60; //锅的底分， 50， 80， 100
            config.paijiu = 0;  // 0 大牌九  1 小牌九

            config.basepoint = 2;
            config.maxpan = 12;
            config.hosttype = 0;
            config.pay = 1;
            config.vip = 0;
            config.pwd = 0;
            config.addratetime = 0;
            config.lod = 0;
        } else if (index == 1) { //小牌九
            config.ruleid = 1; //规则id 1：鬼子+天九王 2：都没有 3：鬼子 4：天九王

            config.guo = 0; // 0 没有锅  1 有锅
            config.xuguo = 0;  // 0 没有续锅  1 有续锅
            config.guomax = 60; //锅的底分， 50， 80， 100
            config.paijiu = 1;  // 0 大牌九  1 小牌九

            config.basepoint = 2;
            config.maxpan = 16;
            config.hosttype = 0;
            config.pay = 1;
            config.vip = 0;
            config.pwd = 0;
            config.addratetime = 0;
            config.lod = 0;
        } else { //加锅牌九
            config.ruleid = 1; //规则id 1：鬼子+天九王 2：都没有 3：鬼子 4：天九王

            config.guo = 1; // 0 没有锅  1 有锅
            config.xuguo = 0;  // 0 没有续锅  1 有续锅
            config.guomax = 60; //锅的底分， 50， 80， 100
            config.paijiu = 0;  // 0 大牌九  1 小牌九

            config.basepoint = 2;
            config.maxpan = 12;
            config.hosttype = 0;
            config.pay = 1;
            config.vip = 0;
            config.pwd = 0;
            config.addratetime = 0;
            config.lod = 0;
        }

        return config;
    };

    that.loadPdkRuleConfig = function (index) {
        var config = {};

        if (index == 0) {
            config.ruleid = 1;
            config.basepoint = 1;
            config.maxpan = 2;
            config.hosttype = 0;        //0红桃三先出 1:赢家先出
            config.alltop = 0;          //0不封顶 1:8张封顶
            config.ruletriple = 0;      //0三带一 1三带二
            config.ruleairplane = 0;    //0单翼 1双翼
            config.rulefour = 0;        //0不能四带三 1可以四带三
            config.vip = 0;
            config.pwd = 1;
            config.lod = 0;
        }

        return config;
    };

    return that;
};

mo.gameRoomRuleHelper = new GameRoomRuleHelper();